/**
 * Created by tdzl2003 on 9/10/16.
 */

import React, { PropTypes } from 'react';
import FormModal from '../../components/FormModal';
import {Form, Input} from 'antd';
import {post} from '../../logics/rpc';
import md5 from "../../logics/md5";
import alert from '../../utils/alert';

@Form.create()
export default class AddAccModal extends React.Component {
  onReturn = ev => {
    this.refs.form.submit();
  };
  fields = {
    account:  {
      rules: [
        { required: true, message: '请输入账号名' },
      ],
    },
    pwd: {
      rules: [
        { required: true, message: '请输入密码' },
      ],
    },
  };
  submit = async ({account, pwd}) => {
    await post('/admin/acc/create', {
      account,
      pwd: md5(pwd),
    });
    alert('创建成功');
    const {onAdded} = this.props;
    onAdded && onAdded();
  };
  render() {
    const { fields } = this;
    const { getFieldProps } = this.props.form;
    return (
      <FormModal
        ref="form"
        title="添加管理员账号"
        okTitle="确 定"
        onSubmit={this.submit}
        {...this.props}
      >
        <Form.Item label="请输入账号名">
          <Input {...getFieldProps('account', fields.account)} onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="请输入密码">
          <Input {...getFieldProps('pwd', fields.pwd)} type="password" onPressEnter={this.onReturn} />
        </Form.Item>
      </FormModal>
    );
  }
}
